﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using System.Data.Common;
using System.Data;
using System.Collections;

namespace AMStudio.Driver
{
    public class PLCPortsTagDAL
    {
        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, PLCPortsTag> SelectAllMapper()
        {
            var list = new Dictionary<string, PLCPortsTag>();
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MachineConfig");
            DataAccessor<PLCPortsTag> myAccessor;
            myAccessor = db.CreateSqlStringAccessor("select * from [PLC_Ports$]",
                MapBuilder<PLCPortsTag>.MapAllProperties().
                 Map(tag => tag.RTDBTag).ToColumn("RTDBTag").
                 Map(tag => tag.RegType).ToColumn("RegType").
                 Map(tag => tag.DataType).WithFunc(f =>
                 {
                     TagDataType t;
                     var name = (string)f["DataType"];
                     switch (name)
                     {
                         case "String":
                             t = TagDataType.String;
                             break;
                         case "Integer":
                             t = TagDataType.Integer;
                             break;
                         case "Real":
                             t = TagDataType.Real;
                             break;
                         case "Discrete":
                         default:
                             t = TagDataType.Discrete;
                             break;
                     }
                     return t;
                 }).
                 Map(tag => tag.Length).ToColumn("Length").
                 Map(tag => tag.BlockAddr).ToColumn("BlockAddr").
                 Map(tag => tag.StartAddr).ToColumn("StartAddr").
                 Map(tag => tag.Index).ToColumn("Index").
                 Map(tag => tag.Name).ToColumn("Name").
                 Map(tag => tag.Direction).WithFunc(f =>
                 {
                     TagDirection d;                     
                      var name = (string)f["Direction"];
                     switch (name)
                     {
                         case "Readable":
                             d = TagDirection.Readable;
                             break;
                         case "Writable":
                             d = TagDirection.Writable;
                             break;
                         case "Both":
                         default:
                             d = TagDirection.Both;
                             break;
                     }
                     return d;
                 }).
                 Map(tag => tag.ReadMode).ToColumn("ReadMode").
                 Map(tag => tag.Modules).ToColumn("Modules").
                 Map(tag => tag.Comments).ToColumn("Comments").
                Build()
                );
            list = myAccessor.Execute()
                .ToDictionary<PLCPortsTag, string>
                ((item) =>
                {
                    return item.Name;
                });
            return list;
        }
    }
}
